/*
 * TDocumentPanel.java
 *
 * Created on April 4, 2007, 10:55 AM
 */

package cotex.working.gui;

import cotex.*;
import cotex.working.*;
import javax.swing.table.TableModel;

/**
 *
 * @author  Ming
 */
public class TDocumentPanel extends javax.swing.JPanel {
    
    /**
     * Creates new form TDocumentPanel
     */
    public TDocumentPanel(TNode node) {
        
        mNode = node;
        
        initComponents();
        
        TWorkingNodeModel nodeModel = (TWorkingNodeModel)mNode.getModel();
        mTable.setDefaultRenderer( TParagraph.class, new TParagraphRenderer() );
        mTable.setDefaultEditor( TParagraph.class, new TParagraphEditor(mNode) );
        mTable.setModel( nodeModel.getData().paragraphs.tableModel );
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        mTable = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        setName("Document");
        mTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Title 1"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        mTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        mTable.setShowHorizontalLines(false);
        mTable.setShowVerticalLines(false);
        mTable.setTableHeader(null);
        mTable.setUpdateSelectionOnSort(false);
        mTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                mTableMousePressed(evt);
            }
        });

        jScrollPane1.setViewportView(mTable);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents

    private void mTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mTableMousePressed
// TODO add your handling code here:
        if(null == mNode)
            return;
        
        int row = mTable.rowAtPoint( evt.getPoint() );
        
        TParagraph paragraph = (TParagraph)mTable.getModel().getValueAt(row, 0);
        
        if( //paragraph.getClass().equals(TContent.class) &&
            paragraph.getState() == TParagraph.State.UNLOCKED) {
        
            mPendingRow = row;
            
            mNode.execute( new TWorkingNodeModel.TLockParagraphCmd(paragraph) );
        }
        
        //TLogManager.logMessage("mTableMousePressed");
        
    }//GEN-LAST:event_mTableMousePressed
            
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable mTable;
    // End of variables declaration//GEN-END:variables
    
    //----------------------------------
    private TNode mNode = null;
    private int mPendingRow = -1;
    
    //----------------------------------
    public void notifyLockResult(boolean result) {
        
        if(result) {
            
            mTable.requestFocus();
            mTable.editCellAt(mPendingRow, 0);
            
            mPendingRow = -1;
        }
        else {
            mTable.editingCanceled(null);
        }
    }
    
    //----------------------------------
    public void notifyCommitResult(boolean result) {
        
        mTable.editingCanceled( null);
        
    }
}
